<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" type="image/png" href="/image/favicon.png">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <title>BOOST - 极速搜索引擎</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        html {
            min-height: 100%;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%) fixed;
        }

        body {
            background: transparent;
            min-height: 100vh;
        }

        .container {
            width: 800px;
            margin: 0 auto;
            padding: 2rem 0;
        }

        .brand {
            text-align: center;
            margin-bottom: 2rem;
        }

        .brand h1 {
            font-size: 3.5rem;
            color: #4e6ef2;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
            letter-spacing: -2px;
            margin-bottom: 0.5rem;
        }

        .brand h1::after {
            content: "🚀";
            margin-left: 1rem;
        }

        .search-wrapper {
            position: relative;
            margin-bottom: 1rem;
        }

        .search {
            width: 100%;
            height: 60px;
            display: flex;
            border-radius: 30px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(78, 110, 242, 0.2);
        }

        .search input {
            flex: 1;
            padding: 1rem 2rem;
            border: none;
            font-size: 1.1rem;
            color: #333;
            background: rgba(255,255,255,0.95);
            transition: all 0.3s ease;
        }

        .search input:focus {
            outline: none;
            background: rgba(255,255,255,1);
            box-shadow: inset 0 0 10px rgba(78, 110, 242, 0.1);
        }

        .search input::placeholder {
            color: #aaa;
            font-style: italic;
        }

        .search button {
            width: 150px;
            border: none;
            background: linear-gradient(45deg, #4e6ef2, #3b5bdb);
            color: white;
            font-size: 1.1rem;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .search button:hover {
            background: linear-gradient(45deg, #3b5bdb, #2a4abf);
            transform: translateY(-2px);
        }

        .search-history {
            position: absolute;
            top: 100%;
            left: 20px;
            right: 20px;
            background: rgba(255,255,255,0.98);
            border-radius: 0 0 15px 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            max-height: 200px;
            overflow-y: auto;
            display: none;
            z-index: 100;
        }

        .search-history:hover {
            display: block;
        }

        .history-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.8rem 2rem;
            color: #666;
            cursor: pointer;
            transition: all 0.2s ease;
            position: relative;
        }

        .history-item > span {
            flex: 1;
        }

        .history-item:hover {
            background: #f8f9fa;
            color: #4e6ef2;
            padding-left: 2.5rem;
        }

        .delete-history {
            background: none;
            border: none;
            color: #999;
            cursor: pointer;
            padding: 0 5px;
            font-size: 1.2rem;
            transition: color 0.2s;
        }

        .delete-history:hover {
            color: #ff4444;
        }

        .result {
            margin-top: 2rem;
            max-height: 60vh;
            overflow-y: auto;
            padding-right: 15px;
        }

        .result::-webkit-scrollbar {
            width: 8px;
            background: rgba(200, 200, 200, 0.1);
        }

        .result::-webkit-scrollbar-thumb {
            background: #c1c1c1;
            border-radius: 4px;
        }

        .item {
            background: white;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            transition: transform 0.3s ease;
        }

        .item:hover {
            transform: translateY(-3px);
        }

        .item a {
            font-size: 1.2rem;
            color: #2c3e50;
            text-decoration: none;
            font-weight: 600;
            line-height: 1.4;
            display: block;
            margin-bottom: 0.5rem;
        }

        .item a:hover {
            color: #4e6ef2;
            text-decoration: underline;
        }

        .item p {
            margin: 0.8rem 0;
            color: #666;
            line-height: 1.6;
            font-size: 0.95rem;
            word-break: break-word;
        }

        .item i {
            color: #27ae60;
            font-size: 0.85rem;
            display: block;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        @keyframes pulse {
            0% { transform: scale(0.9); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 1; }
            100% { transform: scale(0.9); opacity: 0.5; }
        }

        .loading {
            text-align: center;
            padding: 2rem;
            color: #4e6ef2;
        }

        .loading::after {
            content: "⚡";
            display: inline-block;
            animation: pulse 1.5s infinite;
            font-size: 2rem;
            margin-left: 1rem;
        }

        .no-results {
            text-align: center;
            padding: 2rem;
            color: #666;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="brand">
            <h1>BOOST</h1>
        </div>
        <div class="search-wrapper">
            <div class="search">
                <input type="text" placeholder="探索知识的星辰大海..." 
                       id="searchInput"
                       onfocus="showHistory()"
                       onblur="hideHistory()">
                <button onclick="Search()">即刻搜索</button>
            </div>
            <div class="search-history" id="historyPanel"></div>
        </div>
        <div class="result" id="resultContainer"></div>
    </div>

    <script>
        let searchHistory = JSON.parse(localStorage.getItem('searchHistory')) || [];
        let currentQuery = '';

        $(document).ready(function() {
            renderHistory();
            $('#searchInput').on('keypress', function(e) {
                if(e.which === 13) Search();
            });
            
            // 点击页面其他地方时隐藏历史记录
            $(document).on('click', function(e) {
                if (!$(e.target).closest('.search-wrapper').length) {
                    hideHistory();
                }
            });
        });

        function updateHistory(query) {
            if (!query.trim()) return;
            
            currentQuery = query.trim();
            const existingIndex = searchHistory.indexOf(currentQuery);
            
            if (existingIndex !== -1) {
                searchHistory.splice(existingIndex, 1);
            }
            
            searchHistory.unshift(currentQuery);
            
            if (searchHistory.length > 5) {
                searchHistory = searchHistory.slice(0, 5);
            }
            
            localStorage.setItem('searchHistory', JSON.stringify(searchHistory));
            renderHistory();
        }

        function renderHistory() {
            let historyHtml = searchHistory.map(item => {
                const escapedItem = item.replace(/'/g, "\\'");
                return `
                <div class="history-item">
                    <span onclick="handleHistoryClick('${escapedItem}')">${item}</span>
                    <button class="delete-history" onclick="deleteHistory('${escapedItem}', event)">×</button>
                </div>`;
            }).join('');
            
            $('#historyPanel').html(historyHtml || '<div class="history-item">暂无搜索历史</div>');
        }

        function deleteHistory(query, event) {
            event.stopPropagation();
            const index = searchHistory.indexOf(query);
            if (index !== -1) {
                searchHistory.splice(index, 1);
                localStorage.setItem('searchHistory', JSON.stringify(searchHistory));
                renderHistory();
            }
        }

        function handleHistoryClick(query) {
            $('#searchInput').val(query);
            currentQuery = query;
            Search();
            hideHistory();
        }

        function showHistory() {
            if (searchHistory.length > 0) {
                $('#historyPanel').stop(true).slideDown(200);
            }
        }

        function hideHistory() {
            $('#historyPanel').stop(true).slideUp(200);
        }

        function Search() {
            const query = $('#searchInput').val().trim();
            if (!query) return;

            updateHistory(query);
            
            $('#resultContainer').html('<div class="loading">极速搜索中</div>');
            
            $.ajax({
                type: "GET",
                url: "/s?word=" + encodeURIComponent(query),
                success: function(data) {
                    if (data && data.length > 0) {
                        BuildHtml(data);
                    } else {
                        $('#resultContainer').html('<div class="no-results">没有找到相关结果，请尝试其他关键词</div>');
                    }
                },
                error: function() {
                    $('#resultContainer').html('<div class="no-results">搜索服务暂时不可用，请稍后再试</div>');
                }
            });
        }

        function BuildHtml(data) {
            let resultHtml = '';
            
            data.forEach(elem => {
                const maxDescLength = 160;
                const description = elem.desc.length > maxDescLength 
                    ? elem.desc.substring(0, maxDescLength) + '...' 
                    : elem.desc;
                
                resultHtml += `
                    <div class="item">
                        <a href="${elem.url}" target="_blank">${elem.title}</a>
                        <p>${description}</p>
                        <i>${elem.url}</i>
                    </div>
                `;
            });
            
            $('#resultContainer').html(resultHtml);
        }
    </script>
</body>
</html>